<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_caipu extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('Caipu_mdl');
	}

	public function index()
	{
		$this->caipu_list();
	}

	public function caipu_list()
	{
		$keywords = $this->input->post('keywords');
		$page = max(1, intval($this->input->post('pageNum')));
		$limit = 18; //每页显示数
		$offset = ($page - 1) * $limit;


		//分页
		$data['totalCount'] = 183505;
		$data['numPerPage'] = $limit;
		$data['currentPage'] = $page;
		$data['keywords'] = $keywords;
		if ($keywords)
		{
			$data['caipu_list'] = $this->Caipu_mdl->get_caipu_search($keywords, $offset, $limit)->result_array();
		}
		else
		{
			$data['caipu_list'] = $this->Caipu_mdl->get_caipu_list($offset, $limit)->result_array();
		}
		$this->load->view('caipu', $data);
	}

	public function caipu_add()
	{
		if ($this->input->post('submit') == 'ok')
		{
			//格式化用料materials数组
			$materials = $this->input->post('materials');
			$mater = '';
			for($i=0;$i<count($materials['str']);$i++) 
			{ 
				($i > 0) && $mater.='|';
				$mater.=$materials['str'][$i].'，'.$materials['int'][$i];

			}
			//格式化步骤setpInfos数组
			$setpInfos = $this->input->post('setpInfos');
			$setpInfo = '';
			for($i=0;$i<count($setpInfos['str']);$i++) 
			{
				($i > 0) && $setpInfo.='|';
				$setpInfo.=$setpInfos['str'][$i];
			}
			$arr = array(
				'author' => 'admin',
				'authorid' => '1',
				'title' => $this->input->post('title'),
				'costtime' => $this->input->post('costtime'),
				'deal' => $this->input->post('deal'),
				'infos' => $this->input->post('infos'),
				'coverImage' => $this->input->post('coverImage_fileName'),
				'materials' => $mater,
				'setpInfos' => $setpInfo,
				'tips' => $this->input->post('tips'),
				'class_name' => $this->input->post('class_name'),
				'time' => strtotime($this->input->post('time')),
				'views' => $this->input->post('views'),
				'status' => $this->input->post('status'),
			);
			$result = $this->Caipu_mdl->caipu_insert($arr);
			$this->_jui_message('操作成功');
		}
		else
		{
			$data['caipu_info'] = 0;
			$this->load->view('caipu_edit', $data);
		}
	}

	public function caipu_edit($id = '')
	{
		if(empty($id) || !is_numeric($id))
		{
			show_error('失败：参数有误');
			exit();
		}

		if ($this->input->post('submit') == 'ok')
		{
			//格式化用料materials数组
			$materials = $this->input->post('materials');
			$mater = '';
			for($i=0;$i<count($materials['str']);$i++) 
			{ 
				($i > 0) && $mater.='|';
				$mater.=$materials['str'][$i].'，'.$materials['int'][$i];

			}

			//格式化步骤setpInfos数组
			$setpInfos = $this->input->post('setpInfos');
			$setpInfo = '';
			for($i=0;$i<count($setpInfos['str']);$i++) 
			{
				($i > 0) && $setpInfo.='|';
				$setpInfo.=$setpInfos['str'][$i];
			}

			$arr = array(
				'author' => 'admin',
				'authorid' => '1',
				'title' => $this->input->post('title'),
				'costtime' => $this->input->post('costtime'),
				'deal' => $this->input->post('deal'),
				'infos' => $this->input->post('infos'),
				'coverImage' => $this->input->post('coverImage_fileName'),
				'materials' => $mater,
				'setpInfos' => $setpInfo,
				'tips' => $this->input->post('tips'),
				'class_name' => $this->input->post('class_name'),
				'time' => strtotime($this->input->post('time')),
				'views' => $this->input->post('views'),
				'status' => $this->input->post('status'),
			);
			$result = $this->Caipu_mdl->caipu_update($arr, $id);
			$this->_jui_message('操作成功');
		}
		else
		{
			$data['caipu_info'] = $this->Caipu_mdl->get_caipu_by_id($id);

			/** 不存在? */
			if(!$data['caipu_info'])
			{
				redirect('shicai/category');
				exit();
			}
			$data['caipu_setppic'] = $this->Caipu_mdl->get_caipu_attach_by_id($id, 'setp')->result_array();
			$this->load->view('caipu_edit', $data);
		}

	}
	
	public function caipu_del($id = '')
	{
		$result = $this->Caipu_mdl->caipu_delete($id);
		$this->_jui_message('操作成功');
	}
}

/* End of file caipu.php */
/* Location: ./application/controllers/manage/caipu.php */